package cn.tx.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/10/15 21:38
 */
@Configuration
public class DealyMessageConfig {
    @Bean
    public Queue dealyQueue() {
        HashMap<String, Object> args = new HashMap<>();
        // 把一个队列修改为延迟队列
        // 消息的最大存活时间
        args.put("x-message-ttl", 10 * 1000);
        // 该队列里面的消息死了，去那个交换机
        args.put("x-dead-letter-exchange", "DeadLetter.exc");
        // 该队列里面的消息死了，去那个交换机, 由那个路由key 路由他
        args.put("x-dead-letter-routing-key", "DeadLetter.key");
        Queue dealy = new Queue("dealy", true, false, false, args);
        return dealy;
    }

    /**
     * 死信交互就
     * @return
     */
    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange("DeadLetter.exc");
    }

    /**
     * 绑定
     * @return
     */
    @Bean
    public Binding newAndDeadLetterExchange() {
        return BindingBuilder.bind(newQueue()).to(deadLetterExchange()).
                with("DeadLetter.key"); // 死信路由key
    }

    /**
     * 新生队列
     * @return
     */
    @Bean
    public Queue newQueue() {
        return new Queue("new.queue");
    }
}
